package com.lagou.edu.mvcframework.util;

import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SecurityUtil {

    public static final Map<Pattern, Set<String>> SECURY_USERS = new HashMap<Pattern, Set<String>>();

    public static final boolean isUserSecurity(String uri, String user) {

        for (Map.Entry<Pattern, Set<String>> SECURY_USER_ENTRY : SECURY_USERS.entrySet()) {

            Pattern pattern = SECURY_USER_ENTRY.getKey();
            Set<String> users = SECURY_USER_ENTRY.getValue();

            Matcher matcher = pattern.matcher(uri);
            if (matcher.matches()) {

                //如果用户名为空，则返回false
                if (StringUtils.isBlank(user)) {
                    return false;
                }

                //
                if (users.contains(user)) {
                    return true;
                } else {
                    return false;
                }
            }
        }

        //返回true
        return true;
    }
}
